﻿using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using RestSharp;
using TheNorthStar.Api.Requests;
using TheNorthStar.Api.Results;
using NorthStar.Driver.Application;
using Android.Preferences;
using Object = Java.Lang.Object;


namespace NorthStar.Driver
{
    public class DriverLogonAsync : AsyncTask
    {
        private ProgressDialog processDialog;
        private Context m_context;
        private DriverLogon m_driver;

        public DriverLogonAsync( Context context, DriverLogon driver )
        {
            m_context = context;
            m_driver = driver;
        }
        private void SetAlertBox(string title, string msg, string buttonText, Intent intent)
        {
            AlertDialog ad = new AlertDialog.Builder(m_context).Create();
            ad.SetTitle(title);
            ad.SetMessage(msg);
            //ad.SetButton("Yes!", (s, e) => { StartActivity(typeof(MainActivity)); });
            ad.SetButton2(buttonText, (s, e) => { m_context.StartActivity(intent); });
            ad.Show();
        }
        private void SetAlertBox(string title, string msg, string buttonText)
        {
            AlertDialog ad = new AlertDialog.Builder(m_context).Create();
            ad.SetTitle(title);
            ad.SetMessage(msg);
            //ad.SetButton("Yes!", (s, e) => { StartActivity(typeof(MainActivity)); });
            ad.SetButton2(buttonText, (s, e) => { });
            ad.Show();
        }
        private void SetAlertBox(string title, string msg, string buttonText1, string buttonText2, Intent intent)
        {
            AlertDialog ad = new AlertDialog.Builder(m_context).Create();
            ad.SetTitle(title);
            ad.SetMessage(msg);
            ad.SetButton(buttonText1, (s, e) => {
                ChangeDriverStatues();
                m_context.StartActivity(intent);
            });
            ad.SetButton2(buttonText2, (s, e) => { });
            ad.Show();
        }
        private void SetComfirmAlertBox(string carNum, DriverLogonResult result)
        {

            var api = new ConnectToSever(Helper.GetServer(m_context));
            string resultOfCarDetail = api.ComfirmLogginOn(m_driver);
            CarDetails res = Newtonsoft.Json.JsonConvert.DeserializeObject<CarDetails>(resultOfCarDetail);

            if (res.carExists != true)
            {
                this.SetAlertBox("Opps!!!!!!!!", "This Car Number Was Wrong!!!!", "OK");
            }
            else
            {
                string carType = res.carType;
                string seatNum = res.numOfSeats.ToString();
               // MainActivity act = new MainActivity( result.driverId );
                var mact = new Intent(m_context,typeof(MainActivity) );
               
              
                mact.PutExtra( "driverID",  result.driverId.ToString() );
                
                this.SetAlertBox("Comfirm!", "Your car is a: " + carType + " with " + seatNum + " seats??", "Yes", "No", mact);
            }


        }
        private void ChangeDriverStatues()
        {

        }
        protected override void OnPreExecute()
        {
            base.OnPreExecute();
            processDialog = ProgressDialog.Show( m_context, "Driver Loging On...", "Please Wait...", true, true);

        }


        protected override Object DoInBackground(params Object[] @params)
        {
            var api = new ConnectToSever(Helper.GetServer(m_context));
            return api.DriverLogingOn( m_driver );
        }

        protected override void OnPostExecute(Object result)
        {
            
            base.OnPostExecute(result);
            //hide and kill the progress dialog
            processDialog.Hide();
            processDialog.Cancel();
            DriverLogonResult resDriverDetail = Newtonsoft.Json.JsonConvert.DeserializeObject<DriverLogonResult>(result.ToString());

            if (resDriverDetail.logonSuccess)
            {

                this.SetComfirmAlertBox( m_driver.carNum, resDriverDetail );
               
            }
            else
            {

                this.SetAlertBox("Wrong!", "Wrong username or password!!!", "OK!");
            }

        }
    }
    [Activity(Label = "MyDriver-Driver", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            EditText eTextUsername = FindViewById<EditText>(Resource.Id.UserNameBox);
            EditText eTextPassword = FindViewById<EditText>(Resource.Id.PasswordBox);
            EditText eTextCarNum = FindViewById<EditText>(Resource.Id.CarNumBox);
            Button viewPrefsBtn = FindViewById<Button>(Resource.Id.BtnViewPrefs);
            Button button = FindViewById<Button>(Resource.Id.MyButton);
            
            button.Click += delegate 
            {
                if (eTextCarNum.Text != "" && eTextPassword.Text != "" && eTextUsername.Text != "")
                {
                    DriverLogon driver = new DriverLogon();
                    driver.userName = eTextUsername.Text;
                    driver.password = eTextPassword.Text;
                    driver.carNum = eTextCarNum.Text;
                    DriverLogonAsync asyDriver = new DriverLogonAsync(this, driver);
                    asyDriver.Execute();
                }
            };

            viewPrefsBtn.Click += (sender, e) =>
            {
                StartActivity(typeof(PreferencesActivity));
            };

          
        }
    }
}
